import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;

//Test: Last fm api library.
import de.umass.lastfm.*;

@SuppressWarnings("rawtypes")
public class ArtistData implements Comparable
{
    //Instance Variables.
    String bandID;
    ArrayList<UserData> userReferences;
    //ArrayList<String> UserNames; <remove this>
    ArrayList<UserData> ValidUsers;
    Map<UserData,Integer> UserPlayCount;
    int totalCount;
    String name;
    
    //Map<String,String> bandTable;
    
    //Fix filter error?
    Map<String,UserData> UserNameDataMap;
    
    //Artist Breakdown used to calculate data needed for displaying artist information
    ArtistBreakdown theBreakdown;
    
    //Filter stuff here???
    //Build list of valid users.
    //Use the total count function then
        
    public ArtistData(String id)
    {
        bandID = id;
        userReferences = new ArrayList<UserData>();
        //UserNames = new ArrayList<String>(); <remove this>
        UserPlayCount = new HashMap<UserData,Integer>();
        UserNameDataMap = new HashMap<String,UserData>();
        totalCount = 0;
        //name = "artist " + id.substring(0, 15);
        name = "[Undisplayable Name]";
    }
    
    
    
    public void setupBreakdown() {
    	theBreakdown = new ArtistBreakdown(this);
    }
    
    public String getName() {
    	return name;
    	//return bandID; //effort to use MBID for API search IGG
    }
    
    
    //Grab the Music Brainz ID for this artist.
    public String getID()
    {
        return bandID;
    }
    
    //Get how many users this have this artist in their top list.
    public int getUserListSize()
    {
        return userReferences.size();
    }
    
    public int getValidUsersSize() {
    	return ValidUsers.size();
    }
    
    //Get user data via index number
    public UserData getUser(int i) {
    	return userReferences.get(i);
    }
    
    //Add a UserID to the list of UserID tags for this artist.
    //public void addUser(String userName)
    //{
    //    if(!UserNames.contains(userName))
    //    {
    //        UserNames.add(userName);
    //    }
    //}
    
    //Associate the UserID with the number of times played.
    //public void insertUserCount(String userName,String count)
    //{
    //    UserPlayCount.put(userName,Integer.parseInt(count));
    //}
    
    public void insertUserCount(UserData tempUser,String count)
    {
    	UserPlayCount.put(tempUser, Integer.parseInt(count));
    }
    
    
    public void setArtistName(String bandName)
    {
    	name = bandName;
    }
    
    
    //Find out how many times a specific UserID played this artist.
    //public int thisUserListened(String userName)
    //{
    //    return UserPlayCount.get(userName);
    //}


    //Calculuate the total times this artist was played. (Need to adjust it for filters)
    //public void calculateTotalCount() <remove this function>
    //{
    //    totalCount = 0;
    //    for(int index=0; index < UserNames.size(); index++)
    //    {
    //        int value = UserPlayCount.get(UserNames.get(index));
    //        totalCount = totalCount + value;
    //    }
    //    //System.out.println("Artist: " + bandID + " has total plays: " + totalCount);
    //}
    
    public void newCalculateTotal()
    {
        totalCount = 0;
        for(int index=0; index < ValidUsers.size();index++)
        {
            //int value = 0;
            //System.out.println("Value: " + UserPlayCount.get(ValidUsers.get(index).getName()));
        	int value = UserPlayCount.get(ValidUsers.get(index));
            totalCount = totalCount + value;
        }
    }
    
    //Grab the result of calcTotal for this Artist.
    public int getCount()
    {
        return totalCount;
    }
    
    //Get the list of UserID tags for this artist.
    public ArrayList<UserData> getArtistUsers()
    {
        return userReferences;
    }
    
    //Build the mapping from artist to user.
    public void linkUser(UserData currentUser)
    {
        userReferences.add(currentUser);
		String tempUserName = currentUser.getName();
		UserNameDataMap.put(tempUserName, currentUser);
    }
    
    
    //Filter 
    public void applyFilters(Filters dataFilter)
    {
    	//Set ValidUsers to Empty when this Artist is being Ignored.
    	if(GLOBAL.IGNORE.contains(this)) {
    		ValidUsers = new ArrayList<UserData>();
    		this.newCalculateTotal();
    		return;
    	}
    		
        ValidUsers = new ArrayList<UserData>(userReferences);
        //System.out.println("Valid Users: " + ValidUsers.size());//<dflag>
        for(int index=ValidUsers.size()-1; index >=0 ;index--)
        {
            //UserData tempUser = UserNameDataMap.get(ValidUsers.get(index));
            UserData tempUser = userReferences.get(index);
        	if(!tempUser.checkValid(dataFilter))
            {
                ValidUsers.remove(index);
            }
        }
        
        this.newCalculateTotal();
    }
    
    
    
    //TEST function!!!
    public UserData grabUser(int index)
    {
        return userReferences.get(index);
    }
    
    //TEST function!!!
    //public ArrayList<String> 
    
    public int artistTotalCount()
    {
        return totalCount;
    }
    
    
    //Finish writing this!!! <Phil>
    public ArtistBreakdown breakMeDown()
    {
    	//Calculation time!
    	for(int index=0;index < userReferences.size();index++)
    	{
    		UserData tempUser = userReferences.get(index);
    		//GLOBAL.ALL_COUNTRIES
    	}
    	return new ArtistBreakdown();//CHANGE THIS!!!
    }
    
    
    public void createUserNameMap()
    {
    	for(int index=0; index < userReferences.size();index++)
    	{

    	}
    }
    
    
    //Overloaded compare function for comparing two artists....
    public int compareTo(Object obj)
    {
        ArtistData tempArtist = (ArtistData)obj;
        if(totalCount < tempArtist.getCount())
        {
            return 1;
        }
        else if(totalCount == tempArtist.getCount())
        {
            return 0;
        }
        else
        {
            return -1;
        }
    }
    
    
    
    public String toString()
    {
        return bandID;
    }
    
    
}